package com.waken.dorm.common.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.waken.dorm.common.config.OssConstantConfig;
import com.waken.dorm.common.sequence.UUIDSequence;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@Component
public class AliyunOSSUtil {

    @Autowired
    OssConstantConfig ossProperties;

    /**
     * 上传文件到阿里云OSS
     *
     * @return 可访问的路径
     */
    public String upLoad(MultipartFile file, String folderName) throws IOException {
        String bucketName = ossProperties.getBucketName();
        String fileName = file.getOriginalFilename();
        log.info("------OSS文件上传开始--------" + fileName);
        OSSClient client = getOssClient();
        // 创建Bucket
        if (!client.doesBucketExist(bucketName)) {
            client.createBucket(bucketName);
            CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
            // 设置权限(公开读)
            createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
            client.createBucket(createBucketRequest);
        }
        // 设置文件路径和名称
        String newFilePath = ossProperties.getFileHost() + "/" +
                folderName + "/" +
                DateUtils.getCurrentDateTimeAsString("yyyy-MM-dd") + "/" +
                UUIDSequence.next() + "." + FileUtils.getFileSuffix(fileName);
        // 上传文件
        client.putObject(new PutObjectRequest(bucketName, newFilePath, file.getInputStream()));
        return ossProperties.getAccessUrl() + "/" + newFilePath;
    }

    /**
     * 根据key删除OSS服务器上的文件
     *
     * @param filePath 需要删除的文件路径
     * @return
     */
    public void deleteFile(String filePath) {
        log.info("------OSS文件删除开始--------");
        OSSClient ossClient = getOssClient();
        ossClient.deleteObject(ossProperties.getBucketName(), filePath);
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }

    /**
     * 批量删除object
     *
     * @param fileNames 文件名
     */
    public void batchDeleteObject(List<String> fileNames) {
        log.info("------OSS批量文件删除开始--------");
        OSSClient ossClient = getOssClient();
        // 删除Objects
        DeleteObjectsRequest delObjects = new DeleteObjectsRequest(ossProperties.getBucketName()).withKeys(fileNames);
        ossClient.deleteObjects(delObjects);
        // 关闭OSSClient
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }

    /**
     * 判断oss中是否存在文件
     *
     * @param filePath
     * @return
     */
    public Boolean exists(String filePath) {
        log.info("------OSS开始判断文件是否存在--------");
        // 创建OSSClient实例
        OSSClient ossClient = getOssClient();
        Boolean result = ossClient.doesObjectExist(ossProperties.getBucketName(), filePath);
        // 关闭OSSClient
        if (ossClient != null) {
            ossClient.shutdown();
        }
        return result;
    }

    /**
     * 获取oss客户端
     *
     * @return
     */
    private OSSClient getOssClient() {

        return new OSSClient(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
    }
}
